<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="客户编号" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属行业" prop="industry">
        <el-select v-model="queryParams.industry" placeholder="请选择所属行业" clearable>
          <el-option
            v-for="dict in dict.type.py_industry"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.py_suppliers_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customers:customers:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customers:customers:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customers:customers:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customers:customers:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="序号" align="center" prop="id" />
     <el-table-column fixed label="客户编号"  align="center" prop="customerId" width="180px" >
      <template slot-scope="scope">
          <router-link
            :to="{path:'/wms/customers/customers/query',query:{id:scope.row.id}}"
            tag="span"
            style="color: blue; cursor: pointer;"
          >
            {{ scope.row.customerId }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column fixed label="客户名称" align="center" prop="customerName" />
      <el-table-column fixed label="所属行业" align="center" prop="industry">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.py_industry" :value="scope.row.industry"/>
        </template>
      </el-table-column>
      <el-table-column fixed label="客户来源" align="center" prop="source" />
      <el-table-column fixed label="联系人" align="center" prop="contactPerson" />
      <el-table-colum fixed label="电子邮箱" align="center" prop="email" />
      <el-table-column fixed label="销售部门" align="center" prop="deptName" />
      <el-table-column fixed label="销售负责人" align="center" prop="salesPersonId" width="180px"/>
      <el-table-column label="客户地址" align="center" prop="customerAddress" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="创建人" align="center" prop="createPersonId" width="180px"/>
       <el-table-column label="账户名称" align="center" prop="pyAccountList[0].accountName" width="180px"/>
      <el-table-column label="纳税人识别号" align="center" prop="pyAccountList[0].taxId" width="180px"/>
      <el-table-column label="银行账户" align="center" prop="pyAccountList[0].bankAccount" width="180px"/>
      <el-table-column label="开户地址" align="center" prop="pyAccountList[0].accountOpeningAddress" width="180px"/>
      <el-table-column label="开户行地址" align="center" prop="pyAccountList[0].accountAddress" width="180px"/>
      <el-table-column label="制单人" align="center" prop="createPersonId" width="180px"/>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width"></el-table-column> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.py_suppliers_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customers:customers:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customers:customers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="客户编号" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户编号" readonly/>
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="所属行业" prop="industry">
          <el-select v-model="form.industry" placeholder="请选择所属行业">
            <el-option
              v-for="dict in dict.type.py_industry"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="客户来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入客户来源" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="销售部门" prop="salesDepartment">
          <treeselect v-model="form.salesDepartment" :options="deptOptions" :show-count="true" placeholder="请输入销售部门" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="销售负责人" prop="salesPersonId">
          <el-input v-model="form.salesPersonId" placeholder="请输入销售负责人" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <el-form-item label="客户地址" prop="customerAddress">
          <el-input v-model="form.customerAddress" placeholder="请输入客户地址" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
        <!-- <el-form-item label="创建人ID" prop="createPersonId">
          <el-input v-model="form.createPersonId" placeholder="请输入创建人ID" />
        </el-form-item> -->
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.py_suppliers_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">财务信息信息</el-divider>
        <br/>
        <el-row :gutter="20">
          <el-col :span="11">
         <el-form-item label="账户名称" prop="pyAccountList[0].accountName">
        <el-input v-model="form.pyAccount.accountName" placeholder="请输入账户名称" />
        </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="开户银行" prop="pyAccountList[0].bankAccount">
              <el-input v-model="form.pyAccount.bankAccount" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item label="开户地址" prop="pyAccountList[0].accountOpeningAddress">
              <el-input v-model="form.pyAccount.accountOpeningAddress" placeholder="请输入开户地址" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
         <el-form-item label="纳税人识别号" prop="pyAccountList[0].taxId">
          <el-input v-model="form.pyAccount.taxId" placeholder="请输入纳税人识别号" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
         <el-form-item label="银行账户" prop="pyAccountList[0].accountAddress">
          <el-input v-model="form.pyAccount.accountAddress" placeholder="请输入银行账户" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="来往单位" prop="pyAccountList[0].sourceUnit" hidden="hidden">
          <el-input v-model="form.pyAccount.sourceUnit" readonly  />
        </el-form-item>

        <!-- <el-divider content-position="center">财务信息信息</el-divider> -->
        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPyAccount">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePyAccount">删除</el-button>
          </el-col>
        </el-row> -->
        <!-- <el-table :data="pyAccountList" :row-class-name="rowPyAccountIndex" @selection-change="handlePyAccountSelectionChange" ref="pyAccount">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="账户名称" prop="accountName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.accountName" placeholder="请输入账户名称" />
            </template>
          </el-table-column>
          <el-table-column label="开户行地址" prop="accountAddress" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.accountAddress" placeholder="请输入开户行地址" />
            </template>
          </el-table-column>
          <el-table-column label="纳税人识别号" prop="taxId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.taxId" placeholder="请输入纳税人识别号" />
            </template>
          </el-table-column>
          <el-table-column label="银行账户" prop="bankAccount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bankAccount" placeholder="请输入银行账户" />
            </template>
          </el-table-column>
          <el-table-column label="开户地址" prop="accountOpeningAddress" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.accountOpeningAddress" placeholder="请输入开户地址" />
            </template>
          </el-table-column>
          <el-table-column label="逻辑删除" prop="isDelete" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isDelete" placeholder="请输入逻辑删除" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomers, getCustomers, delCustomers, addCustomers, updateCustomers, getCode } from "@/api/customers/customers";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Customers",
  dicts: ['py_industry', 'py_suppliers_status'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPyAccount: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      customersList: [],
      // 财务信息表格数据
      pyAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        industry: null,
        contactPerson: null,
        status: null
      },
      // 表单参数
      form: {
        createPersonId: null,
        supplierId: null,
        supplierCode: null,
        industryId: null,
        supplierCategoryId: null,
        contactPerson: null,
        supplierName: null,
        contactPhone: null,
        email: null,
        purchaseDepartmentId: null,
        purchaseResponsibleId: null,
        status: null,
        supplierAddress: null,
        remarks: null,
        manager: null,
        createdAt: null,
        updatedAt: null,
        isDeleted: null,
        pyAccount: {
        accountName: null,
        taxId: null,
        accountAddress: null,
        bankAccount: null,
        accountOpeningAddress: null,
        sourceUnit: null
      },
        number:undefined,
        codeId:undefined
      },
      // 表单校验
      rules: {
        customerId: [
          {required: true, message: "客户编号不能为空", trigger: "change"}
        ],
        industry: [
          {required: true, message: "所属行业不能为空", trigger: "change"}
        ],
        contactPerson: [
          {required: true, message: "联系人不能为空", trigger: "change"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        customerName: [
          {required: true, mesage: "客户名称不能为空", trigger: "change"}
        ],
        email: [{required: true, mesage: "邮箱不能为空", trigger: "change"}
        ],
        customerAddress: [
          {required: true, mesage: "销售负责人不能为空", trigger: "change"}
        ],
      },
      deptOptions: undefined
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listCustomers(this.queryParams).then(response => {
        this.customersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        customerName: null,
        industry: null,
        source: null,
        contactPerson: null,
        email: null,
        salesDepartment: null,
        salesPersonId: null,
        customerAddress: null,
        notes: null,
        createPersonId: null,
        createTime: null,
        updatePersonId: null,
        updateTime: null,
        isDeleted: null,
        status: null,
        pyAccount: {
        accountName: null,
        taxId: null,
        accountAddress: null,
        bankAccount: null,
        accountOpeningAddress: null,
        sourceUnit: null
         }
      };
      this.pyAccountList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
       /** 获取编号*/
        getCode(10).then(response => {
        console.log(response);
        this.form.customerId=response.sequence;
        this.form.number=response.number;
        this.form.codeId=response.codeId;
    });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomers(id).then(response => {
        for(let yi in this.customersList) {
            if(this.customersList[yi].pyAccountList[0].sourceUnit === response.data.customerId) {
              //alert( this.customersList[yi].pyAccountList[0].accountId);
               this.form.id = this.customersList[yi].id;
               this.form.customerName = this.customersList[yi].customerName;
               this.form.customerId = this.customersList[yi].customerId;
               this.form.industry = this.customersList[yi].industry;
               this.form.source = this.customersList[yi].source;
               this.form.contactPerson = this.customersList[yi].contactPerson;
               this.form.email = this.customersList[yi].email;
               this.form.salesDepartment = this.customersList[yi].salesDepartment;
               this.form.salesPersonId = this.customersList[yi].salesPersonId;
               this.form.customerAddress = this.customersList[yi].customerAddress;
               this.form.notes = this.customersList[yi].notes;
               this.form.status = this.customersList[yi].status;
               this.form.pyAccount.sourceUnit = this.customersList[yi].pyAccountList[0].sourceUnit;
               this.form.pyAccount.accountId = this.customersList[yi].pyAccountList[0].accountId;
               this.form.pyAccount.accountName = this.customersList[yi].pyAccountList[0].accountName;
               this.form.pyAccount.remarks = this.customersList[yi].pyAccountList[0].remarks;
               this.form.pyAccount.taxId = this.customersList[yi].pyAccountList[0].taxId;
               this.form.pyAccount.accountOpeningAddress = this.customersList[yi].pyAccountList[0].accountOpeningAddress;
               this.form.pyAccount.sourceUnit = this.customersList[yi].pyAccountList[0].sourceUnit;
               this.form.pyAccount.bankAccount = this.customersList[yi].pyAccountList[0].bankAccount;
               this.form.pyAccount.accountAddress = this.customersList[yi].pyAccountList[0].accountAddress;
          }
        }
        this.open = true;
        this.title = '修改供应商信息';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.pyAccountList = this.pyAccountList;
          if (this.form.id != null) {
            this.form.pyAccount.sourceUnit = this.form.customerId;
            updateCustomers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.pyAccount.sourceUnit = this.form.customerId;
            addCustomers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户信息编号为"' + id + '"的数据项？').then(function() {
        alert(id);
        return delCustomers(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 财务信息序号 */
    rowPyAccountIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 财务信息添加按钮操作 */
    handleAddPyAccount() {
      let obj = {};
      obj.accountName = "";
      obj.accountAddress = "";
      obj.taxId = "";
      obj.bankAccount = "";
      obj.accountOpeningAddress = "";
      obj.isDelete = "";
      this.pyAccountList.push(obj);
    },
    /** 财务信息删除按钮操作 */
    handleDeletePyAccount() {
      if (this.checkedPyAccount.length == 0) {
        this.$modal.msgError("请先选择要删除的财务信息数据");
      } else {
        const pyAccountList = this.pyAccountList;
        const checkedPyAccount = this.checkedPyAccount;
        this.pyAccountList = pyAccountList.filter(function(item) {
          return checkedPyAccount.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePyAccountSelectionChange(selection) {
      this.checkedPyAccount = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customers/customers/export', {
        ...this.queryParams
      }, `customers_${new Date().getTime()}.xlsx`)
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    }
  }
};
</script>
